package com.SouthernWall_404.age_tool.common.world.Entity;

import com.SouthernWall_404.age_tool.AgeTool;
import com.SouthernWall_404.age_tool.common.API.NBT.MusicBlockInfo;
import com.SouthernWall_404.age_tool.common.Network.ModChannels;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.HandShake.MusicBlockNBTHandShakePacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.HandShake.MusicBlockStartPlayHandShakePacket;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.MusicBlockNBTC2SPacket;
import com.SouthernWall_404.age_tool.common.init.ModBlockEntities;
import com.SouthernWall_404.age_tool.common.world.Inventory.MusicBlockMenu;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;

public class MusicBlockEntity extends BlockEntity implements MenuProvider {

    private static final String MUSIC_PATH="music_path";//音乐路径的定义名称
    private static final String RADIUS="radius";//播放范围的定义名称
    private static final String VOLUMN="volumn";//播放范围的定义名称


    private CompoundTag modPack;



    private final ItemStackHandler inventory=new ItemStackHandler(9)
    {
        @Override
        protected void onContentsChanged(int slot) {
            setChanged();
        }
    };

    public MusicBlockEntity(BlockEntityType<?> p_155228_, BlockPos p_155229_, BlockState p_155230_) {
        super(p_155228_, p_155229_, p_155230_);

    }

    public ItemStackHandler getInventory()
    {
        return inventory;
    }

    public MusicBlockEntity(BlockPos pos, BlockState blockState) {
        super(ModBlockEntities.MUSIC_BLOCK_ENTITY.get(),pos, blockState);
    }
    @Override
    public Component getDisplayName() {
        return Component.translatable("test");
    }

    @Override
    public @Nullable AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {//注意，接下来以此代码为基础进行研究！！！！！！！！！
        //return ChestMenu.threeRows(id, inventory, new SimpleContainer(27));
        return new MusicBlockMenu(id,inventory,this.getBlockPos());
    }


    @Override
    protected void saveAdditional(CompoundTag nbt) {
        if(nbt!=null)
        {
            super.saveAdditional(nbt);
            if(modPack!=null)
            {
                nbt.put(AgeTool.MODID,modPack);
            }
        }

    }
    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        this.modPack=(CompoundTag) nbt.get(AgeTool.MODID);

    }

     public void setNBT(CompoundTag modPack) {
        if(modPack!=null)
        {
            this.modPack = modPack;
            this.setChanged(); // 标记数据已修改
            if (level != null) {
                // 通知客户端区块更新
                level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), Block.UPDATE_ALL);
        }
         }
     }

     /*
     记住，不要再用this.modPack直接读取了！
     可同步；
      */

     public CompoundTag getOrCreateModPack()
     {
         if(level.isClientSide)//如果是客户端
         {
             ModChannels.sendToServer(new MusicBlockNBTHandShakePacket(getBlockPos()));//拉取数据


//             Minecraft.getInstance().player.sendSystemMessage(Component.literal(level+":\n"+this.modPack));
             /*
             测试里，记得删
              */
         }

         return this.modPack;//返回该内容
     }

     /*
     用于重设modPack
     注意用这个!
      */

    public void putModPackBE(CompoundTag modPack)
    {

        this.setNBT(modPack);//设置当前nbt

        if(this.level.isClientSide)//如果是客户端
        {
            BlockPos blockPos=this.getBlockPos();//获取当前坐标
            ModChannels.sendToServer(new MusicBlockNBTC2SPacket(modPack,blockPos));//将修改同步到服务端
        }

    }

    /*
    用于编辑设置
     */
    public void setMusicBlockSetting(ResourceLocation musicLocation,int radius,float volumn)
    {

        CompoundTag modPack=new CompoundTag();//建立新的modPack
        modPack.putString(MUSIC_PATH,musicLocation.toString());//填充内容
        modPack.putInt(RADIUS,radius);
        modPack.putFloat(VOLUMN,volumn);

        this.putModPackBE(modPack);//存入ModPack
    }

    /*
    用于获取当前设置
     */
    public MusicBlockInfo getMusicBlockSetting()
    {
        CompoundTag modPack=this.getOrCreateModPack();//获取当前modPack
        if(modPack==null)
        {
            return new MusicBlockInfo();
        }
        MusicBlockInfo musicBlockInfo =new MusicBlockInfo();//初始化设置
        musicBlockInfo.musicLocation=new ResourceLocation(modPack.getString(MUSIC_PATH));//输入路径
        musicBlockInfo.radius=modPack.getInt(RADIUS);//输入播放半径
        musicBlockInfo.volumn=modPack.getFloat(VOLUMN);

        return musicBlockInfo;//输出
    }

    /*
    播放方法
    可以重复播放
    自带刷新
     */
    public void startPlay()
    {

//            content.getSender().sendSystemMessage(Component.literal(level+":\n"+modPack));

        if(level.isClientSide)//如果是客户端
        {
            boolean isBlockPowered = level.hasNeighborSignal(this.getBlockPos());
//            if(isBlockPowered)
            {
                ModChannels.sendToServer(new MusicBlockStartPlayHandShakePacket(this.getBlockPos()));
            }
        }

    }


}


